/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 应用路由测试
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import '../../lib/utils/app_routes.dart';
import '../../lib/viewmodels/music_player_view_model.dart';
import '../../lib/viewmodels/music_search_view_model.dart';

void main() {
  group('AppRoutes 测试', () {
    testWidgets('路由映射表包含所有必要的路由', (WidgetTester tester) async {
      // 验证路由映射表
      final routes = AppRoutes.routes;

      expect(routes.containsKey(AppRoutes.musicSearch), true);
      expect(routes.containsKey(AppRoutes.musicPlayer), true);
      expect(routes.containsKey(AppRoutes.playlist), true);
    });

    testWidgets('路由常量定义正确', (WidgetTester tester) async {
      expect(AppRoutes.home, '/');
      expect(AppRoutes.musicSearch, '/music_search');
      expect(AppRoutes.musicPlayer, '/music_player');
      expect(AppRoutes.playlist, '/playlist');
    });

    testWidgets('路由生成器能正确处理音乐播放页面参数', (WidgetTester tester) async {
      const routeSettings = RouteSettings(
        name: '/music_player',
        arguments: {
          'music': null,
          'autoPlay': true,
        },
      );

      final route = AppRoutes.onGenerateRoute(routeSettings);
      expect(route, isNotNull);
      expect(route, isA<MaterialPageRoute>());
    });

    testWidgets('路由生成器能正确处理播放列表页面参数', (WidgetTester tester) async {
      const routeSettings = RouteSettings(
        name: '/playlist',
        arguments: {
          'initialIndex': 5,
        },
      );

      final route = AppRoutes.onGenerateRoute(routeSettings);
      expect(route, isNotNull);
      expect(route, isA<MaterialPageRoute>());
    });

    testWidgets('路由生成器对未知路由返回 null', (WidgetTester tester) async {
      const routeSettings = RouteSettings(
        name: '/unknown_route',
      );

      final route = AppRoutes.onGenerateRoute(routeSettings);
      expect(route, isNull);
    });
  });

  group('路由导航方法测试', () {
    late Widget testApp;

    setUp(() {
      testApp = MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (_) => MusicPlayerViewModel()),
          ChangeNotifierProvider(create: (_) => MusicSearchViewModel()),
        ],
        child: MaterialApp(
          routes: AppRoutes.routes,
          onGenerateRoute: AppRoutes.onGenerateRoute,
          home: const Scaffold(
            body: Text('Home Page'),
          ),
        ),
      );
    });

    testWidgets('toMusicSearch 方法能正确导航', (WidgetTester tester) async {
      await tester.pumpWidget(testApp);

      final context = tester.element(find.text('Home Page'));
      await AppRoutes.toMusicSearch(context);
      await tester.pumpAndSettle();

      // 验证导航到了音乐搜索页面
      expect(find.text('音乐搜索'), findsOneWidget);
    });

    testWidgets('toMusicPlayer 方法能正确导航', (WidgetTester tester) async {
      await tester.pumpWidget(testApp);

      final context = tester.element(find.text('Home Page'));
      await AppRoutes.toMusicPlayer(context);
      await tester.pumpAndSettle();

      // 验证导航到了音乐播放页面
      expect(find.byType(Scaffold), findsWidgets);
    });

    testWidgets('toPlaylist 方法能正确导航', (WidgetTester tester) async {
      await tester.pumpWidget(testApp);

      final context = tester.element(find.text('Home Page'));
      await AppRoutes.toPlaylist(context);
      await tester.pumpAndSettle();

      // 验证导航到了播放列表页面
      expect(find.byType(Scaffold), findsWidgets);
    });
  });
}
